package cn.itrip.service.Impl;

import cn.itrip.DTO.ItripListCommentDTO;
import cn.itrip.DTO.ItripScoreCommentDTO;
import cn.itrip.dao.ItripCommentMapper;
import cn.itrip.dao.ItripHotelOrderMapper;
import cn.itrip.dao.ItripImageMapper;
import cn.itrip.entity.ItripComment;
import cn.itrip.entity.ItripImage;
import cn.itrip.service.ItripCommentService;
import cn.itrip.utils.BigDecimalUtil;
import cn.itrip.utils.Constants;
import cn.itrip.utils.EmptyUtils;
import cn.itrip.utils.Page;
import cn.itrip.vo.biz.ItripSearchCommentVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
@Service
public class ItripCommentServiceImpl extends ServiceImpl<ItripCommentMapper,ItripComment> implements ItripCommentService {

    @Resource
    private ItripImageMapper itripImageMapper;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    @Override
    public List<ItripComment> getCommentByHotelId(Long hotelId) {
        return baseMapper.getCommentByHotelId(hotelId);
    }

    @Override
    public Integer getCommentIsOkCount(Long hotelId) {
        return baseMapper.getCommentIsOkCount(hotelId);
    }

    @Override
    public Integer getCommentHavingImgCount(Long hotelId) {
        return baseMapper.getCommentHavingImgCount(hotelId);
    }

    @Override
    public ItripScoreCommentDTO getAvgAndTotalScore(Long hotelId) {
        return baseMapper.getAvgAndTotalScore(hotelId);
    }

    @Override
    public Page<ItripListCommentDTO> queryItripCommentPageByMap(ItripSearchCommentVO itripSearchCommentVO) throws Exception {
        Integer total = baseMapper.getItripCommentCountByMap(itripSearchCommentVO);
        Integer pageNo = EmptyUtils.isEmpty(itripSearchCommentVO.getPageNo()) ? Constants.DEFAULT_PAGE_NO : itripSearchCommentVO.getPageNo();
        Integer pageSize = EmptyUtils.isEmpty(itripSearchCommentVO.getPageSize()) ? Constants.DEFAULT_PAGE_SIZE : itripSearchCommentVO.getPageSize();
        Page page = new Page(pageNo, pageSize, total);
        List<ItripListCommentDTO> itripCommentList = baseMapper.getCommentPageByHotelId(itripSearchCommentVO);
        page.setRows(itripCommentList);
        return page;
    }

    @Override
    public boolean itriptxAddItripComment(ItripComment obj, List<ItripImage> itripImages) throws Exception {
        if (null != obj) {
            //计算综合评分，综合评分=(设施+卫生+位置+服务)/4
            float score = 0;
            int sum = obj.getFacilitiesScore() + obj.getHygieneScore() + obj.getPositionScore() + obj.getServiceScore();
            score = BigDecimalUtil.OperationASMD(sum, 4, BigDecimalUtil.BigDecimalOprations.divide, 1, BigDecimal.ROUND_DOWN).floatValue();
            //对结果四舍五入
            obj.setScore(Math.round(score));
            Long commentId = 0L;
            if (baseMapper.insertItripComment(obj) > 0) {
                commentId = obj.getId();
                if (null != itripImages && itripImages.size() > 0 && commentId > 0) {
                    for (ItripImage itripImage : itripImages) {
                        itripImage.setTargetId(commentId);
                        itripImageMapper.insertItripImage(itripImage);
                    }
                }
                //更新订单表-订单状态为4（已评论）
                itripHotelOrderMapper.updateHotelOrderStatus(obj.getOrderId(), obj.getCreatedBy());
                return true;
            }
        }
        return false;
    }
}
